XY Plots

rich_data <- read.csv("datasets//richPK.csv")
names(rich_data) <- toupper(names(rich_data))
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(PKPDmisc)

mean_ct <- rich_data %>% group_by(TIME) %>% summarize(CONC = mean(CONC)) 



p_conc_time <- ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID)) + 
  geom_line() + 
  geom_point(size=2.5) + 
  geom_line(data = mean_ct, 
            aes(x = TIME, y = CONC, group=NULL), 
            size = 2,  color = "red") + 
  xlab("Time (hours)") +
  ylab("Concentration (mg/L)")

# regular scale
p_conc_time

plot of chunk unnamed-chunk-1

# log scale
p_conc_time + scale_y_log10()

plot of chunk unnamed-chunk-1

p_conc_time + facet_wrap(~RACE) + base_theme_obs()

plot of chunk unnamed-chunk-1

  1. Basic Concentration-Time plot (point and lines)
ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID)) + 
  geom_line() + 
  geom_point()

plot of chunk unnamed-chunk-2

  1. make points/lines bigger
ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID)) + 
  geom_line(size = 1.25) + # add size
  geom_point(size = 3)  # add size control

plot of chunk unnamed-chunk-3

  1. add log transformation to y axis
ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID)) + 
  geom_line(size = 1.25) + 
  geom_point(size = 3) + 
  scale_y_log10() # add scale

plot of chunk unnamed-chunk-4 5) Add color by gender

ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID, color = GENDER)) + 
  geom_line(size = 1.25) + 
  geom_point(size = 3) 

plot of chunk unnamed-chunk-5

  1. facet by race
ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID, color = GENDER)) + 
  geom_line(size = 1.25) + 
  geom_point(size = 3) +
  facet_wrap(~RACE) # use facet_wrap or facet_grid

plot of chunk unnamed-chunk-6

  1. add mean conc-time profile and color red
p_conc_time <- ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID)) + 
  geom_line() + 
  geom_point(size=2.5) 

# calculate mean concentration time profile
mean_ct <- rich_data %>% group_by(TIME) %>% summarize(CONC = mean(CONC)) 

p_conc_time + 
  # add new line and specify new data to draw this line
  geom_line(data = mean_ct, 
            #don't forget group=NULL since will be looking to group by ID due to original specification in the base plot
            aes(x = TIME, y = CONC, group=NULL), 
            size = 2,  color = "red") + 
  # add some better label names too
  xlab("Time (hours)") +
  ylab("Concentration (mg/L)")

plot of chunk unnamed-chunk-7

  1. add two mean profiles (one for each gender)
mean_ct_gender <- rich_data %>% group_by(TIME, GENDER) %>% summarize(CONC = mean(CONC)) 

p_conc_time + 
  # add new line and specify new data to draw this line
  geom_line(data = mean_ct_gender, 
            # remember to move color into aes since you're specifying
            #a column in the dataset to specify the coloring
            aes(x = TIME, y = CONC, group=NULL,  color = GENDER), 
            size = 2) + 
  # add some better label names too
  xlab("Time (hours)") +
  ylab("Concentration (mg/L)")

plot of chunk unnamed-chunk-8

  1. color by weight
ggplot(data = rich_data, 
                      aes(x = TIME, y = CONC, group=ID, color = WEIGHT)) + 
  geom_line(size = 1.25) + 
  geom_point(size = 3)

plot of chunk unnamed-chunk-9

Boxplots and Histograms

# filter to one observation per ID
ggplot(data = rich_data %>% filter(!duplicated(ID)), 
       aes(x = WEIGHT)) + 
  geom_histogram()
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

plot of chunk unnamed-chunk-10

# clean up with customizations in geom_histogram
ggplot(data = rich_data %>% filter(!duplicated(ID)), 
       aes(x = WEIGHT)) + 
  geom_histogram(binwidth= 4, color="black", fill="white")

plot of chunk unnamed-chunk-10

median <- rich_data %>% filter(!duplicated(ID)) %>% summarize(medianWT = median(WEIGHT))
ggplot(data = rich_data %>% filter(!duplicated(ID)), 
       aes(x = WEIGHT)) + 
  geom_histogram(binwidth= 4, color="black", fill="white") +
  geom_vline(xintercept = median[["medianWT"]], size= 2, color = "red")

plot of chunk unnamed-chunk-11

cmaxauc <- rich_data %>% group_by(ID, GENDER) %>% summarize(cmax = max(CONC), aucinf = AUC_inf(TIME, CONC))
ggplot(cmaxauc, aes(x = GENDER, y = cmax, group = GENDER)) + geom_boxplot()

plot of chunk unnamed-chunk-12

ggplot(cmaxauc, aes(x = GENDER, y = aucinf, group = GENDER)) + geom_boxplot()

plot of chunk unnamed-chunk-12

Eta-Eta plots

Read in EtaCov_base dataset

etacov_base <- read.csv("datasets/EtaCov_base.csv")
library(GGally)
## Warning: package 'GGally' was built under R version 3.1.1
ggpairs(etacov_base %>% select(nV, nCl, nKa),
        lower=list(continuous="smooth", params=c(colour="black")),
        diag=list(continuous="density", params=c(colour="black", fill = "white")), axisLabels = "show")

plot of chunk unnamed-chunk-14

etacov_base$Scenario <- NULL
library(reshape2)
enc_melt <- melt(etacov_base, id.vars=c("ID", "WT"))
gg_covwt <- ggplot(enc_melt, aes(x = WT, y=value, group = ID)) 
gg_covwt+ geom_point() + 
  facet_wrap(~variable)

plot of chunk unnamed-chunk-15

gg_covwt+ geom_point() + 
  facet_wrap(~variable)+ stat_smooth(aes(group= variable), se=F, size = 1.25)
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

plot of chunk unnamed-chunk-16